package com.xunshibao.bll.message;

import java.util.List;

import com.xunshibao.core.dao.TransactionItem;
import com.xunshibao.core.dao.page.Pagination;
import com.xunshibao.entity.message.Message;
import com.xunshibao.service.message.MessageCatType;
import com.xunshibao.service.message.MessageSourceType;
import com.xunshibao.service.message.MessageStatus;

public interface MessageLogic {
	/**
	 * 发送消息持久化,SQL语句：<br/>
	 * <code>INSERT INTO  `pw_ms_messages`   SET   `create_uid`  =  '2' ,  `create_username`  =  'haiker' ,  `title`  =  'hello admin' ,  `content`  =  'Hi admin, I&#39;m haiker nice to meet you.' ,  `extra`  =  'a:1:{i:0;s:5:\"admin\";}' ,  `expand`  =  'a:2:{s:10:\"categoryid\";i:1;s:6:\"typeid\";i:100;}' ,  `modified_time`  =  '1294800398' ,  `created_time`  =  '1294800398'</code>
	 * @param senderId 发件人编号
	 * @param sendDate 发送日期
	 * @param senderName 发件人用户名
	 * @param title 消息标题
	 * @param content 消息内容
	 * @return 事务处理对象
	 */
	Message saveMessages(Long senderId, Integer sendDate, String senderName, String title, String content);
	
	/**
	 * 消息关系对象，用来保存发件人给谁发送了消息，SQL语句<br/>
	 * <code>
	 * INSERT INTO pw_ms_relations (uid,mid,categoryid,typeid,status,isown,created_time,modified_time) VALUES   ('1','3','1','100','1','0','1294800397','1294800397') , ('2','3','1','100','0','1','1294800397','1294800397')
	 * </code>
	 * @param senderId 发件人编号
	 * @param receiverId 收件人编号
	 * @param msgId 消息编号
	 * @param sendDate 发送日期
	 * @param type 消息类型：系统消息，站内消息
	 * @return 事务处理对象
	 */
	List<TransactionItem> saveRelations(Long senderId, Long receiverId, Long msgId, Integer sendDate, MessageCatType type);
	
	/**
	 * 收件人回复消息，SQL语句<br/>
	 * <code>
	 * INSERT INTO  `pw_ms_replies`   SET   `parentid`  =  '3' ,  `create_uid`  =  '2' ,  `create_username`  =  'haiker' ,  `title`  =  'hello admin' ,  `content`  =  'Hi admin, I&#39;m haiker nice to meet you.' ,  `status`  =  '1' ,  `modified_time`  =  '1294800397' ,  `created_time`  =  '1294800397'
	 * </code>
	 * @param msgId 消息编号
	 * @param createUid 回复人编号
	 * @param replyDate 回复日期
	 * @param createUsername 回复人用户名
	 * @param title 回复消息的标题
	 * @param content 回复消息的内容
	 * @return 事务处理对象
	 */
	TransactionItem saveReplies(Long msgId, Long createUid, Integer replyDate, String createUsername, String title, String content);
	
	/**
	 * 更新收件人新消息的条数，不用每次都去count表，提高性能，SQL语句<br/>
	 * <code>
	 * UPDATE pw_ms_configs SET sms_num = sms_num+ '1'  WHERE uid in('1')<br/>
	 * UPDATE pw_members SET newpm=newpm+1 WHERE uid= '1'
	 * </code>
	 * 
	 * @param uid 用户编号
	 * @param type 消息类型，站内新、系统消息、用户通知等
	 * @return 事务处理对象数组
	 */
	List<TransactionItem> updateMsgNums(Long uid, MessageCatType type);
	
	/**
	 * 获取指定用户的消息数量，SQL语句<br/>
	 * <code>
	 * 全部消息数 SELECT COUNT(*) as total FROM pw_ms_relations force index(idx_uid_categoryid_modifiedtime) WHERE uid =  '1'  AND categoryid= '1'  ORDER BY modified_time DESC LIMIT 1<br/>
	 * 未读消息数 SELECT COUNT(*) as total FROM pw_ms_relations WHERE uid =  '1'  AND categoryid= '1'  AND status= '1'  ORDER BY modified_time DESC LIMIT 1
	 * </code>
	 * @param userId 用户编号
	 * @param cat 消息类型
	 * @param source 表示消息的来源类型，是收件箱还是发件箱
	 * @param isNew 是否未读消息数 
	 * @return
	 */
	int msgCount(Long userId, MessageCatType cat, MessageSourceType source, boolean isNew);
	
	/**
	 * 取得消息关联关系列表，因为发送的消息和收到的消息都存放在一张表里的，都可以在这里取，SQL语句<br/>
	 * <code>
	 * SELECT * FROM pw_ms_relations force index(idx_uid_categoryid_modifiedtime) WHERE uid =  '1'  AND categoryid= '1'  ORDER BY modified_time DESC LIMIT 0,20
	 * </code>
	 *  new MsgRelation(r.relationId, r.msgid, r.status)
	 * @param userId 用户编号
	 * @param cat 消息类型
	 * @param source 发件人还是收件人
	 * @param pageNo 页数
	 * @param pageSize 页大小
	 * @return 消息关系数组对象
	 */
	Pagination getMsgRelation(Long userId, MessageCatType cat, MessageSourceType source, int pageNo, int pageSize) ;
	
	/**
	 * 取得消息列表，SQL语句<br/>
	 * <code>
	 * SELECT * FROM pw_ms_messages WHERE mid in ('4','3')
	 * </code>
	 * @param msgIds 消息编号数组
	 * @param pageNo 页数
	 * @param pageSize 页大小
	 * @return 消息对象数组
	 */
	List<Message> getMsgList(List<Long> msgIds);
	
	/**
	 * 重置用户的消息通知，用来显示在登录后，用户有多少条新消息，SQL语句<br/>
	 * <code>
	 * UPDATE pw_ms_configs SET   `sms_num` ='0' WHERE uid in(  '1')
	 * </code>
	 * @param userIds 用户编号数组
	 * @return 事务处理对象
	 */
	TransactionItem resetNewMsgCount(List<Long> userIds);
	
	/**
	 * 根据用户编号列表，取得各个用户的消息总数，SQL语句<br/>
	 * <code>
	 * SELECT COUNT(*) as total,uid FROM pw_ms_relations WHERE uid in( '1') AND typeid in( '100','401') GROUP BY uid ORDER BY null
	 * </code>
	 * @param userIds 用户编号数组
	 * @return 事务处理对象
	 */
	TransactionItem[] userMsgCounts(List<Long> userIds);
	
	/**
	 * 当用户在查看消息时，更新回复消息的状态为已读，SQL语句<br/>
	 * <code>
	 * UPDATE pw_ms_replies SET   `status` ='0'WHERE id in ('2')
	 * </code>
	 * @param replyIds
	 * @param status 消息的状态
	 * @return
	 */
	TransactionItem updateReplyStatus(List<Long> replyIds, MessageStatus status);
	
	/**
	 * 更新回复消息被查看的时间和状态，SQL语句<br/>
	 * <code>
	 * UPDATE pw_ms_relations SET   `actived_time` ='1294802413',  `status` ='0' WHERE rid in ( '5' ) AND uid= '1'
	 * </code>
	 * @param uid 查看消息的用户编号
	 * @param replyIds 回复消息编号
	 * @param replyDate 回复日期
	 * @return
	 */
	TransactionItem updateReplyViewDate(Long uid, List<Long> replyIds, Integer replyDate);
	
	/**
	 * SQL语句<br/>
	 * <code>
	 * UPDATE pw_ms_relations SET   `actived_time` ='1294802413',  `status` ='0' WHERE rid in ( '5' ) AND uid= '1'
	 * </code>
	 * @param mid
	 * @param uid
	 * @param replyDate
	 * @param content
	 * @return
	 */
	List<TransactionItem> replyMsg(Long mid, Long uid, String username, Integer replyDate, String content);
	
	/**
	 * 删除用户的信息
	 * @param rid 用户的信息编号
	 * @param uid 用户编号
	 * @return
	 */
	List<TransactionItem> delete(Long rid, Long uid, Long mid);
}
